package com.fjec.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fjec.base.entity.SysAnnex;
import com.fjec.base.service.IBizLogicService;
import com.fjec.base.service.ISysAnnexService;
import com.fjec.common.protocol.JsonResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BizLogicServiceImpl implements IBizLogicService {

    @Resource
    private ISysAnnexService sysAnnexService;

    @Override
    public List<String> getRotatePic(Integer bizType, Long bizId) {
        QueryWrapper<SysAnnex> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("delFlag", 0)
                .eq("bizType", bizType)
                .eq("bizId", bizId)
                .orderByAsc("sort");
        List<SysAnnex> entityList = sysAnnexService.list(queryWrapper);
        List<String> resultList = new ArrayList<>();
        for (SysAnnex entity : entityList) {
            resultList.add(entity.getUrl());
        }
        return resultList;
    }

    @Override
    public List<Map<String, Object>> getRotatePicV1(Integer bizType, Long bizId) {
        QueryWrapper<SysAnnex> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("delFlag", 0)
                .eq("bizType", bizType)
                .eq("bizId", bizId)
                .orderByAsc("sort");
        List<SysAnnex> entityList = sysAnnexService.list(queryWrapper);
        List<Map<String, Object>> resultList = new ArrayList<>();
        Map<String, Object> tmpMap;
        for (SysAnnex entity : entityList) {
            tmpMap = new HashMap<>();
            tmpMap.put("url", entity.getUrl());
            tmpMap.putAll(entity.getBizData());
            resultList.add(tmpMap);
        }
        return resultList;
    }

    @Override
    public JsonResult getBackgroundPic(Long bizId, Integer bizType, List<Integer> bizCodeList) {
        QueryWrapper<SysAnnex> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("delFlag", 0)
                .eq("bizId", bizId)
                .eq("bizType", bizType)
                .in("bizCode", bizCodeList)
                .orderByAsc("sort");
        List<SysAnnex> entityList = sysAnnexService.list(queryWrapper);
        List<Map<String, Object>> resultList = new ArrayList<>();
        Map<String, Object> tmpMap;
        for (SysAnnex entity : entityList) {
            tmpMap = new HashMap<>();
            tmpMap.put("url", entity.getUrl());
            tmpMap.put("bizCode", entity.getBizCode());
            tmpMap.putAll(entity.getBizData());
            resultList.add(tmpMap);
        }
        return JsonResult.success()
                .data(resultList)
                .build();
    }
}
